//
//  NewsImageListViewController.m
//  movie
//
//  Created by 李丹阳 on 15/7/22.
//  Copyright (c) 2015年 李丹阳. All rights reserved.
//

#import "NewsImageListViewController.h"
#import "baseNavigationController.h"
#import "baseViewController.h"
#import "photoViewController.h"

#define Kwidth  self.view.bounds.size.width
#define Kheight self.view.bounds.size.height

@interface NewsImageListViewController ()

@end

@implementation NewsImageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];//复写方法，一定要调用super
    [self loadData];
    [self _createCollectionView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_createCollectionView{
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake( (Kwidth-50)/4,(Kheight-60)/6);
    
    imageView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, Kheight-55) collectionViewLayout:layout ];
    
    imageView.delegate = self;
    imageView.dataSource = self;
    
    [imageView registerClass:[imageCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:imageView];
    
}

- (void)loadData{
    
    imageData = [[NSMutableArray alloc]init];
    NSArray* jsonArray = [dataService getJSONDataFromFile:@"image_list"];
    for (NSDictionary* dic in jsonArray) {
        ImageModal* modal = [[ImageModal alloc]init];
        [modal  setValuesForKeysWithDictionary:dic];
        [imageData addObject:modal];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return imageData.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    imageCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.modal = imageData[indexPath.row];
    return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    photoViewController* vc = [[photoViewController alloc]init];
    vc.view.backgroundColor = [UIColor redColor];
    vc.index = indexPath.row;
    baseNavigationController* nav = [[baseNavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];

    NSMutableArray* imageUrlArray = [[NSMutableArray alloc]init];
    for (int i = 0; i <imageData.count; i++) {
        ImageModal* modal = imageData[i];
        NSString* str = modal.image;
        [imageUrlArray addObject:str];
    }
    vc.imageUrlArray = imageUrlArray;

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
